package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.chen.domain.dto.R;
import org.chen.domain.entity.ApiKeyEntity;
import org.chen.service.ApiKeyService;
import org.chen.vo.QuotaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/system")
public class ApiKeyController {
    @Autowired
    private ApiKeyService apiKeyService;
    
    @GetMapping("/api-keys")
    public R<IPage<ApiKeyEntity>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return R.ok(apiKeyService.page(new Page<>(current, size)));
    }
    
    @PostMapping("/api-keys")
    public R<Boolean> add(@RequestBody ApiKeyEntity apiKey) {
        return R.ok(apiKeyService.saveApiKey(apiKey));
    }
    
    @PutMapping("/api-keys/{id}")
    public R<Boolean> update(@PathVariable Long id, @RequestBody ApiKeyEntity apiKey) {
        apiKey.setId(id);
        return R.ok(apiKeyService.updateById(apiKey));
    }
    
    @DeleteMapping("/api-keys/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(apiKeyService.removeById(id));
    }
    
    @PostMapping("/api-keys/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return R.ok(apiKeyService.removeByIds(ids));
    }
    
    @GetMapping("/api-keys/{id}/quota")
    public R<QuotaVO> getQuota(@PathVariable Long id) {
        return R.ok(apiKeyService.getQuota(id));
    }
}